package com.usermanage.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by  on 2018/9/30
 */
public class DateUtil {

    /**
     * 返回当前电脑时间 yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getNowTimeString() {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time = format.format(date);
        return time;
    }

    /**
     * 按照传入的格式返回当前电脑时间
     *
     * @return
     */
    public static String getNowTime(String str) {
        Date date = new Date();
        DateFormat format = new SimpleDateFormat(str);
        return format.format(date);
    }


    /**
     * 返回当前日期 yyyy-MM-dd
     *
     * @return
     */
    public static String getNowDayString() {
        SimpleDateFormat dfday = new SimpleDateFormat("yyyy-MM-dd");
        String nowday = dfday.format(new Date());
        return nowday;
    }

    /**
     * 将13位时间戳转为 yyyy-MM-dd
     *
     * @param time
     * @return
     */
    public static String timeStampToDate(String time) {
        Long timeLong = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static String formatDate(String date) {
        DateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date result = format1.parse(date);
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            String dateString = formatter.format(result);
            return dateString;
        } catch (ParseException e) {
            e.printStackTrace();
            return null;
        }

    }

    public static String formatDateByDate(Date date) {
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(date);
        return dateString;
    }

    /**
     * 返回当前日期 yyyyMMddHHmmss
     *
     * @return
     */
    public static String getNowString() {
        SimpleDateFormat dfday = new SimpleDateFormat("yyyyMMddHHmmss");
        String nowday = dfday.format(new Date());
        return nowday;
    }
}
